﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using TianYu.CMS.Domain.DomainModel;
using TianYu.CMS.Infrastructure.Enums;
using TianYu.CMS.Service;
using TianYu.Core.Common;
using TianYu.Core.Common.BaseViewModel;

namespace TianYu.CMS.WebMvc.Areas.Admin.Controllers
{
    public class SysMenuController : BaseController
    {
        private ISysMenuService _sysMenuService;

        public SysMenuController(ISysMenuService sysMenuService)
        {
            this._sysMenuService = sysMenuService;
        }



        public IActionResult Index()
        {
            return View();
        }
        public IActionResult AddEdit(int? id, int? pid)
        {
            var model = new SysMenu()
            {
                Id = id.HasValue ? id.Value : 0,
                ParentId = pid.HasValue ? pid.Value : -1,
                Enabled = true
            };
            if (id.HasValue && id > 0)
            {
                model = _sysMenuService.FindById(id);
            }
            ViewBag.ParentName = "顶级";
            List<SysMenu> list = _sysMenuService.Query(t => t.Id != model.Id && t.Status == StatusEnum.Effective).ToList();
            if (pid.HasValue && pid > 0)
            {
                var parentMenu = _sysMenuService.FindById(pid);

                if (parentMenu != null)
                {
                    ViewBag.ParentName = parentMenu.MenuName;
                    if (!id.HasValue)
                        model.MenuType = parentMenu.MenuType + 1;
                }
            }
            else
            {
                model.MenuType = 1;
            }

            return View(model);
        }
        public IActionResult SelectIcon()
        {
            return View();
        }



        public async Task<AjaxResult<List<SysMenu>>> GetList()
        {
            var list = await _sysMenuService.QueryAsync(o => o.Status == StatusEnum.Effective, o => o.Sort, OrderByType.Asc);

            return list.ToViewModel();
        }
        public async Task<AjaxResult<bool>> Save(SysMenu model)
        {
            if (_sysMenuService.Any(o => o.Id != model.Id && o.MenuName == model.MenuName && o.ParentId == model.ParentId && o.GroupId == model.GroupId && o.Status == StatusEnum.Effective))
            {
                return AjaxResult<bool>.Fail($"菜单名称[{model.MenuName}]已存在，不能重复添加");
            }

            if (model.Id > 0)
            {
                var entity = _sysMenuService.FindByClause(x => x.Id == model.Id && x.Status == 0);

                if (entity == null)
                {
                    return AjaxResult<bool>.Fail("菜单不存在或已被删除");
                }

                var result = await _sysMenuService.UpdateAsync(o => new SysMenu()
                {
                    MenuIcon = model.MenuIcon,
                    MenuName = model.MenuName,
                    MenuType = model.MenuType,
                    MenuUrl = model.MenuUrl,
                    Level = model.Level,
                    JsEvent = model.JsEvent,
                    GroupId = model.GroupId,
                    BackColor = model.BackColor,
                    IsToolbar = model.IsToolbar,
                    Enabled = model.Enabled,
                    SizeStyle = model.SizeStyle,
                    Sort = model.Sort,
                    Modifier = LoginUserName,
                    ModifyGuid = LoginUserGuid,
                    ModifyTime = DateTime.Now

                }, x => x.Id == model.Id);

                return result ? AjaxResult<bool>.Success() : AjaxResult<bool>.Fail("保存失败");
            }
            else
            {
                var maxCode = await _sysMenuService.FindMaxCode(model.ParentId);

                if (model.ParentId == -1)
                {
                    model.ParentCode = 0;
                    model.MenuCode = maxCode + 10;
                    model.Level = 1;
                }
                else
                {
                    var parentMenu = await _sysMenuService.FindByClauseAsync(x => x.Id == model.ParentId);

                    model.ParentCode = parentMenu.MenuCode;
                    model.Level = parentMenu.Level + 1;
                    model.MenuCode = maxCode == 0 ? parentMenu.MenuCode * 100 + 10 : maxCode + 1;
                }

                model.Enabled = true;

                model.Enabled = true;
                model.CreateTime = DateTime.Now;
                model.CreateGuid = LoginUserGuid;
                model.Creator = LoginUserName;
                await _sysMenuService.InsertAsync(model);

                return AjaxResult<bool>.Success();
            }
        }
        public async Task<AjaxResult<bool>> Remove(int id)
        {
            var res = await _sysMenuService.UpdateAsync(o => new SysMenu()
            {
                Status = StatusEnum.Invalid,
                Modifier = LoginUserName,
                ModifyGuid = LoginUserGuid,
                ModifyTime = DateTime.Now
            }, x => x.Id == id);

            return res ? AjaxResult<bool>.Success() : AjaxResult<bool>.Fail("保存失败");
        }
        public async Task<AjaxResult<bool>> UpdateSort(int id, int sort)
        {
            var result = await _sysMenuService.UpdateAsync(o => new SysMenu() { Sort = sort }, x => x.Id == id);

            return result ? AjaxResult<bool>.Success() : AjaxResult<bool>.Fail("保存失败");
        }
    }
}